import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mi_mang_flutter/provider/app_theme.dart';

const double buttonHeight = 50;

class MiMangAppTheme {
  static TextTheme _buildTextTheme(
    TextTheme base,
    String fontFamily, {
    bool isDark = false,
  }) {
    Color textColor = isDark ? Colors.white : Colors.black;
    Color displayColor = isDark ? Colors.white70 : Colors.black87;
    Color bodyColor = isDark ? Colors.white60 : Colors.black54;
    Color labelColor = isDark ? Colors.white70 : Colors.black87;

    return base.copyWith(
      displayLarge: AppTheme.displayLarge.copyWith(
        color: displayColor,
        fontFamily: fontFamily,
      ),
      displayMedium: AppTheme.displayMedium.copyWith(
        color: displayColor,
        fontFamily: fontFamily,
      ),
      displaySmall: AppTheme.displaySmall.copyWith(
        color: displayColor,
        fontFamily: fontFamily,
      ),
      headlineLarge: AppTheme.headlineLarge.copyWith(
        color: textColor,
        fontFamily: fontFamily,
      ),
      headlineMedium: AppTheme.headlineMedium.copyWith(
        color: textColor,
        fontFamily: fontFamily,
      ),
      headlineSmall: AppTheme.headlineSmall.copyWith(
        color: textColor,
        fontFamily: fontFamily,
      ),
      titleLarge: AppTheme.titleLarge.copyWith(
        color: textColor,
        fontFamily: fontFamily,
      ),
      titleMedium: AppTheme.titleMedium.copyWith(
        color: textColor,
        fontFamily: fontFamily,
      ),
      titleSmall: AppTheme.titleSmall.copyWith(
        color: textColor,
        fontFamily: fontFamily,
      ),
      bodyLarge: AppTheme.bodyLarge.copyWith(
        color: bodyColor,
        fontFamily: fontFamily,
      ),
      bodyMedium: AppTheme.bodyMedium.copyWith(
        color: bodyColor,
        fontFamily: fontFamily,
      ),
      bodySmall: AppTheme.bodySmall.copyWith(
        color: bodyColor,
        fontFamily: fontFamily,
      ),
      labelLarge: AppTheme.labelLarge.copyWith(
        color: labelColor,
        fontFamily: fontFamily,
      ),
      labelMedium: AppTheme.labelMedium.copyWith(
        color: labelColor,
        fontFamily: fontFamily,
      ),
      labelSmall: AppTheme.labelSmall.copyWith(
        color: labelColor,
        fontFamily: fontFamily,
      ),
    );
  }

  static ThemeData customeThemeData(
    bool isDark,
    Color? themeColor,
    String fontFamily,
  ) {
    Brightness brightness = isDark ? Brightness.dark : Brightness.light;
    SystemUiOverlayStyle systemUiOverlayStyle =
        isDark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;
    Color? primaryColor = themeColor;
    Color secondaryColor = isDark ? Colors.grey[700]! : Colors.white;
    ColorScheme baseScheme = isDark ? ColorScheme.dark() : ColorScheme.light();
    ColorScheme colorScheme = baseScheme.copyWith(
      primary: primaryColor,
      secondary: secondaryColor,
      brightness: brightness,
    );
    ThemeData baseTheme = isDark ? ThemeData.dark() : ThemeData.light();
    TextTheme defaultTextTheme =
        _buildTextTheme(baseTheme.textTheme, fontFamily, isDark: isDark);

    return baseTheme.copyWith(
      platform: TargetPlatform.iOS,
      textTheme: defaultTextTheme,
      brightness: brightness,
      primaryColor: primaryColor,
      colorScheme: colorScheme,
      indicatorColor: baseTheme.indicatorColor,
      highlightColor: const Color(0x40CCCCCC),
      splashColor: const Color(0x40CCCCCC),
      splashFactory: InkRipple.splashFactory,
      canvasColor: isDark ? AppTheme.darkCanvasColor : AppTheme.canvasColor,
      scaffoldBackgroundColor: isDark
          ? AppTheme.darkScaffoldBackgroundColor
          : AppTheme.scaffoldBackgroundColor,
      buttonTheme: ButtonThemeData(
        colorScheme: colorScheme,
        textTheme: ButtonTextTheme.primary,
        height: buttonHeight,
        highlightColor: const Color(0x40CCCCCC),
        splashColor: const Color(0x40CCCCCC),
      ),
      textButtonTheme: TextButtonThemeData(
        style: flatButtonStyle.copyWith(
          foregroundColor: WidgetStatePropertyAll(
            isDark ? AppTheme.darkButtonColor : AppTheme.buttonColor,
          ),
          overlayColor: const WidgetStatePropertyAll(Colors.transparent),
        ),
      ),
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: raisedButtonStyle.copyWith(
          backgroundColor: WidgetStatePropertyAll(primaryColor),
          foregroundColor: WidgetStatePropertyAll(
            isDark ? Colors.white : Colors.black,
          ),
        ),
      ),
      outlinedButtonTheme: OutlinedButtonThemeData(
        style: outlineButtonStyle.copyWith(
          foregroundColor: WidgetStatePropertyAll(primaryColor),
          side: WidgetStatePropertyAll(
            BorderSide(color: primaryColor ?? Colors.transparent),
          ),
        ),
      ),
      inputDecorationTheme: InputDecorationTheme(
        labelStyle: defaultTextTheme.titleMedium,
        hintStyle: defaultTextTheme.bodyMedium,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(4.0),
        ),
      ),
      tabBarTheme: TabBarTheme(
        labelColor: primaryColor,
        unselectedLabelColor: isDark ? Colors.white70 : Colors.black87,
        labelStyle: defaultTextTheme.labelLarge,
        unselectedLabelStyle: defaultTextTheme.labelLarge,
      ),
      appBarTheme: AppBarTheme(
        backgroundColor: isDark ? const Color(0xFF222222) : primaryColor,
        elevation: 0.5,
        systemOverlayStyle: systemUiOverlayStyle,
        titleTextStyle: defaultTextTheme.titleLarge,
      ),
      dividerTheme: DividerThemeData(
        color: isDark ? AppTheme.darkDividerColor : AppTheme.dividerColor,
        indent: 15.0,
        space: 0.5,
        thickness: 0.5,
      ),
      primaryTextTheme: defaultTextTheme,
      cupertinoOverrideTheme: CupertinoThemeData(
        brightness: brightness,
        textTheme: CupertinoTextThemeData(
          textStyle: defaultTextTheme.titleLarge,
          actionTextStyle: defaultTextTheme.titleLarge,
          tabLabelTextStyle: defaultTextTheme.titleLarge,
          navActionTextStyle: defaultTextTheme.titleLarge,
          navTitleTextStyle: defaultTextTheme.titleLarge,
          navLargeTitleTextStyle: defaultTextTheme.titleLarge,
          pickerTextStyle: defaultTextTheme.titleLarge,
          dateTimePickerTextStyle: defaultTextTheme.titleLarge,
        ),
      ),
      typography: Typography.material2018(platform: TargetPlatform.iOS),
    );
  }
}

final ButtonStyle flatButtonStyle = TextButton.styleFrom(
  minimumSize: Size(88, buttonHeight),
  padding: const EdgeInsets.symmetric(horizontal: 16.0),
  shape: const StadiumBorder(),
  foregroundColor: Colors.black87,
);

final ButtonStyle raisedButtonStyle = ElevatedButton.styleFrom(
  minimumSize: Size(88, buttonHeight),
  padding: const EdgeInsets.symmetric(horizontal: 16),
  shape: const StadiumBorder(),
  elevation: 2,
);

final ButtonStyle outlineButtonStyle = OutlinedButton.styleFrom(
  minimumSize: Size(88, buttonHeight),
  padding: const EdgeInsets.symmetric(horizontal: 16),
  shape: const StadiumBorder(),
);
